package com.apitest.utils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @program: cuke
 * @description: 文件读写
 * @author: shixing
 * @create: 2021-01-12 16:05
 **/
public class FsKit {

    /**
     * 输出文件
     *
     * @param name 文件名
     * @param code 内容
     * @throws Exception
     */
    public static void writeCode(String name, String code) throws IOException {
            OutputStream os =  new FileOutputStream(name);
            OutputStreamWriter oStreamWriter = new OutputStreamWriter(os, "utf-8");
            System.out.println("正在生成：" + name);
            oStreamWriter.append(code);
            oStreamWriter.close();
    }
    public static void writeCode(String path,String name, String code) throws IOException {
            File file= new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            OutputStream os =  new FileOutputStream(path+name);
            OutputStreamWriter oStreamWriter = new OutputStreamWriter(os, "utf-8");
            System.out.println("正在生成：" + name);
            oStreamWriter.append(code);
            oStreamWriter.close();

    }

    public static String readCode(String path) {
        StringBuffer code = new StringBuffer();
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "utf-8");
            BufferedReader read = new BufferedReader(reader);
            while (read.readLine() != null) {
                code.append(read.readLine());
            }
            read.close();
            reader.close();
            return code.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getMimeType(String filePath) {
        Path uri = Paths.get(filePath);
        try {
            String type = Files.probeContentType(uri);
            return type;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getResourcePath(){
        String path = FsKit.class.getResource("/").getPath();
        //判断是否linux：linux系统目录加根目录斜杠
        if(FsKit.isWindows()){
            return path.substring(1);
        }else {
            return path;
        }
    }
    public static String getResourcePath(String fileName){
        String path = FsKit.class.getResource("/"+fileName).getPath();
        //判断是否linux：linux系统目录加根目录斜杠
        if(FsKit.isWindows()){
            return path.substring(1);
        }else {
            return path;
        }
    }
    public static String getTargetPath(){
        String path = FsKit.getResourcePath();
        return path.substring(0, path.indexOf("target/") + "target/".length());

    }
    public static String getTargetPath(String fileName){
        String path = FsKit.getResourcePath();
        return path.substring(0, path.indexOf("target/") + "target/".length()) + fileName;
    }



    /**
     * 检查目录
     *
     * @param filePath
     */
    public static void filePathExist(String filePath) {
        File file = new File(filePath);
        //如果文件夹不存在则创建
        if (!file.exists() && !file.isDirectory()) {
            System.out.println("//不存在");
            file.mkdir();
        } else {
            System.out.println("//目录存在");
        }
    }


    /**
     * 判断当前系统类型是否windows
     * @return
     */
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("win");
    }

    public static String  projectPath(){
        return  System.getProperty("user.dir")+
                File.separatorChar +
                "src"+
                File.separatorChar +
                "test" +
                File.separatorChar +
                "resources";
    }
}

